package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.system.domain.BiddingDemand;
import com.ruoyi.system.domain.BiddingDemandSelection;
import com.ruoyi.system.mapper.BiddingDemandMapper;
import com.ruoyi.system.mapper.BiddingDemandSelectionMapper;
import com.ruoyi.system.service.BiddingDemandSelectionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

@Service
public class BiddingDemandSelectionServiceImpl implements BiddingDemandSelectionService {
    @Resource
    private BiddingDemandSelectionMapper biddingDemandSelectionMapper;

    @Resource
    private BiddingDemandMapper biddingDemandMapper;


    /**
     * @param biddingDemandSelection 投标实体类
     * @return 返回查询到的结果
     * @Deprecated 查询某个招标中的所有投标人信息
     */
    @Override
    public List<BiddingDemandSelection> queryAllDemandSelectionList(BiddingDemandSelection biddingDemandSelection) {
        return biddingDemandSelectionMapper.selectAllDemandSelectionList(biddingDemandSelection);
    }

    /**
     * @param biddingDemand 招标实体类
     * @return 返回查询到的结果
     * @Deprecated 查询某个招标的详情信息
     */
    @Override
    public BiddingDemand queryBiddingDemandInfo(BiddingDemand biddingDemand) {
        return biddingDemandMapper.selectByDemandId(biddingDemand);
    }

    /**
     * @param biddingDemand 招标实体类
     * @return 返回结果
     * @Deprecated 投标
     */
    @Override
    @Transactional
    public AjaxResult myBid(BiddingDemand biddingDemand, LoginUser loginUser) {
        // 查询是不是自己接自己的单
        if (Objects.equals(biddingDemand.getUserId(), loginUser.getUserId()))
            return AjaxResult.error("无法投递自己发布的任务!");
        // 查询是否重复投标
        int total = biddingDemandSelectionMapper.selectIsRepeat(biddingDemand.getBiddingDemandId(),loginUser.getUserId());
        if (total != 0)
            return AjaxResult.error("你已参与此任务,无法继续投递!");
        // 插入投标
        BiddingDemandSelection insert = new BiddingDemandSelection();
        insert.setBiddingDemandId(biddingDemand.getBiddingDemandId());
        insert.setBiddingSelectionUserName(loginUser.getUsername());
        insert.setBiddingSelectionUserId(loginUser.getUserId());
        insert.setBiddingSelectionType("0");
        insert.setBiddingSelectionTotalMoney(new BigDecimal(1));
        insert.setBiddingSelectionGuaranteeMoney(new BigDecimal(1));
        biddingDemandSelectionMapper.insertSelective(insert);
        // 修改这个任务的状态 如果总条数大于0 表示有人在投递 如果为0 表示只有一个人 可以直接设置为投标中
        int TotalCount = biddingDemandSelectionMapper.selectTotalCount(biddingDemand.getBiddingDemandId());
        BiddingDemand queryOn = biddingDemandMapper.selectByDemandId(biddingDemand);
        if (TotalCount > 0){
            queryOn.setStatus("1");
        }else {
            queryOn.setStatus("0");
        }
        biddingDemandMapper.updateByPrimaryKey(queryOn);
        return AjaxResult.success();
    }
}
